package com.leo.foodmaster.admin.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.SysDictData;
import com.leo.foodmaster.admin.system.service.ISysDictDataService;
import com.leo.foodmaster.admin.system.service.ISysDictTypeService;
import com.leo.foodmaster.commons.annotation.Inner;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-29
 */
@RestController
@Api(tags = "字典数据管理")
@AllArgsConstructor
@RequestMapping("/system/dict/data")
public class SysDictDataController {
    private final ISysDictTypeService dictTypeService;
    private final ISysDictDataService dictDataService;


    /**
     * 根据字典类型查询字典数据信息
     */
    @Inner
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public Result<List<SysDictData>> getByDictType(@ApiParam("字典类型") @PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (data.isEmpty()) {
            data = new ArrayList<>();
        }
        return Result.success(data);
    }

    @GetMapping("/list")
    @ApiOperation("分页条件查询字典数据列表")
    public Result<IPage<SysDictData>> getDictTypePageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "字典类型") @RequestParam(required = false) String dictType,
            @ApiParam(value = "字典数据名称") @RequestParam(required = false) String name,
            @ApiParam(value = "字典数据状态") @RequestParam(required = false) String status) {
        IPage<SysDictData> pageList  = dictDataService.getDictDataPageList(new Page<>(pageNum, pageSize),
                dictType, name, status);
        return Result.success(pageList);
    }

    @PostMapping
    @ApiOperation("新增字典数据")
    public Result<Object> addSysDictData(@RequestBody SysDictData dictData) {
        return dictDataService.addSysDictData(dictData);
    }

    @PutMapping
    @ApiOperation("修改字典数据")
    public Result<Object> updateSysDictData(@RequestBody SysDictData dictData) {
        return dictDataService.updateSysDictData(dictData);
    }

    @DeleteMapping("/{dictDataIds}")
    @ApiOperation("删除字典数据")
    public Result<Object> deleteSysDictData(@ApiParam("字典数据Ids") @PathVariable String dictDataIds) {
        return dictDataService.deleteSysDictData(dictDataIds);
    }

}

